<?php

if( !defined('UserPermission_class') )
{
  define('UserPermission_class', 'true', false);
  
  class UserPermission extends databaseData
  {
	var $connection;
	var $uid;
	var $database;
	var $user_obj;
	var $userperm_obj;
	var $deptperm_obj;
	var $FORBIDDEN_RIGHT;
	var $NONE_RIGHT;
	var $VIEW_RIGHT;
	var $READ_RIGHT;
	var $WRITE_RIGHT;
	var $ADMIN_RIGHT;

	function UserPermission($uid, $connection, $database)
	{
		$this->uid = $uid;
		$this->connection = $connection;
		$this->database = $database;
		$this->user_obj = new User($this->uid, $this->connection, $this->database);
		$this->userperm_obj = new User_Perms($this->user_obj->getId(), $connection, $database);
		$this->deptperm_obj = new Dept_Perms($this->user_obj->getDeptId(), $connection, $database);
		$this->FORBIDDEN_RIGHT = $this->userperm_obj->FORBIDDEN_RIGHT;
		$this->NONE_RIGHT = $this->userperm_obj->NONE_RIGHT;
		$this->VIEW_RIGHT = $this->userperm_obj->VIEW_RIGHT;
		$this->READ_RIGHT = $this->userperm_obj->READ_RIGHT;
		$this->WRITE_RIGHT = $this->userperm_obj->WRITE_RIGHT;
		$this->ADMIN_RIGHT = $this->userperm_obj->ADMIN_RIGHT;
	}

	function getAllowedFileIds()
	{
		$start_time = time();
		$viewable_array = $this->getViewableFileIds();
		echo '<br> <b> Load Viewable Time: ' . (time() - $start_time) . ' </b>';
		$start_time = time();
		$readable_array = $this->getReadableFileIds();
		echo '<br> <b> Load Readable Time: ' . (time() - $start_time) . ' </b>';
		$start_time = time();
		$writeable_array = $this->getWriteableFileIds();
		echo '<br> <b> Load Writable Time: ' . (time() - $start_time) . ' </b>';
		$start_time = time();
		$adminable_array = $this->getAdminableFileIds();
		echo '<br> <b> Load Admin Time: ' . (time() - $start_time) . ' </b>';
		$start_time = time();
		$result_array = array_values( array_unique( array_merge($viewable_array, $readable_array, $writeable_array, $adminable_array) ) );
		echo '<br> <b> 3 combines Time: ' . (time() - $start_time) . ' </b><br>';
		return $result_array;
	}
	 
	function getAllowedFileOBJs()
	{	
                return $this->convertToFileDataOBJ( $this->getAllowedFileIds() );	
    }
   	function getViewableFileIds()
	{
		$array = array();
		
		$userperm_filearray = ($this->userperm_obj->getCurrentViewOnly());
		$deptperm_filearray = ($this->deptperm_obj->getCurrentViewOnly());
		$query = "SELECT {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER_PERMS.fid FROM {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA,
				{$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER_PERMS WHERE ({$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER_PERMS.uid = $this->uid
				AND {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA.id = {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER_PERMS.fid
				AND {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_USER_PERMS.rights < $this->VIEW_RIGHT
				AND {$GLOBALS['CONFIG']['db_prefix']}$this->TABLE_DATA.publishable = 1)";
		$result = mysql_query($query, $this->connection) or die('Unable to query: ' . $query .
				'Error: ' . mysql_error());
		$len = mysql_num_rows($result);
		for($index=0; $index < $len; $index++)
		{
			list($array[$index]) = mysql_fetch_row($result);
		}
		$deptperm_filearray = array_diff($deptperm_filearray, $array);
		$deptperm_filearray = array_diff($deptperm_filearray, $userperm_filearray);
		$total_listing = array_merge($userperm_filearray , $deptperm_filearray);
		//$total_listing = array_unique( $total_listing);
		//$result_array = array_values($total_listing);
		return $total_listing;
	}

	function getViewableFileOBJs()
	{	
    	return $this->convertToFileDataOBJ($this->getViewableFileIds());	
    }
 
	function getReadableFileIds()
	{
		$userperm_filearray = $this->userperm_obj->getCurrentReadRight();
		$deptperm_filearray = $this->deptperm_obj->getCurrentReadRight();
		$published_filearray = $this->user_obj->getPublishedData(1);
		$result_array = array_values( array_unique( array_merge($published_filearray, $userperm_filearray, $deptperm_filearray) ) );
		return $result_array;
	}
	
	function getReadableFileOBJs()
	{	
    	return $this->convertToFileDataOBJ($this->getReadableFileIds());	
    }
	 
	function getWriteableFileIds()
	{
		$userperm_filearray = $this->userperm_obj->getCurrentWriteRight();
		$deptperm_filearray = $this->deptperm_obj->getCurrentWriteRight();
		$published_filearray = $this->user_obj->getPublishedData(1);
		$result_array = array_values( array_unique( array_merge($published_filearray, $userperm_filearray, $deptperm_filearray) ) );
		return $result_array;
	}
	 
	function getWriteableFileOBJs()
	{	
        return $this->convertToFileDataOBJ($this->getWriteableFileIds());	
    }
	function getAdminableFileIds()
	{
		$userperm_filearray = $this->userperm_obj->getCurrentAdminRight();
		$deptperm_filearray = $this->deptperm_obj->getCurrentAdminRight();
		$published_filearray = $this->user_obj->getPublishedData(1);
		$result_array = array_values( array_unique( array_merge($published_filearray, $userperm_filearray, $deptperm_filearray) ) );
		return $result_array;
	}
	function getAdminableFileOBJs()
	{	
        return $this->convertToFileDataOBJ($this->getAdminableFileIds());	
    }
	
	function combineArrays($high_priority_array, $low_priority_array)
	{
		
		return databaseData::combineArrays($high_priority_array, $low_priority_array);
	}
	
	function getAuthority($data_id)
	{
		$file_obj = new FileData($data_id, $GLOBALS['connection'], $GLOBALS['database']);
		if($this->user_obj->isRoot())
			return $this->ADMIN_RIGHT;
		if($file_obj->isOwner($this->uid) && $file_obj->isLocked() )
			return $this->WRITE_RIGHT;
		$uperm = $this->userperm_obj->getPermission($data_id);
		$dperm = $this->deptperm_obj->getPermission($data_id);
		if( $uperm>=$this->userperm_obj->NONE_RIGHT and $uperm <= $this->userperm_obj->ADMIN_RIGHT)
			return $uperm;
		else
			return $dperm;
	}
  }
}
?>
